package com.gxa.financial.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.financial.mapper.AccountListMapper;
import com.gxa.financial.service.AccountListService;
import com.gxa.common.R;
import com.gxa.common.entity.financial_management.AccountList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class AccountListServiceImpl extends ServiceImpl<AccountListMapper, AccountList> implements AccountListService {

    @Autowired
    private AccountListMapper accountListMapper;

    //分页查询
    @Cacheable(value = "accountList",key = "'selectAccountList'")
    @Override
    public R selectAccountList(Integer currentPage, Integer pageSize) {
        Page page = new Page(currentPage,pageSize);
        Page selectPage = accountListMapper.selectPage(page, null);
        R r = new R();
        if (selectPage.getRecords() != null) {
            Map accountListMap = new HashMap();
            accountListMap.put("totalSize",selectPage.getTotal());
            accountListMap.put("totalPage",selectPage.getPages());
            accountListMap.put("totalData",selectPage.getRecords());
            r.setCode(200);
            r.setMessage("查询成功");
            r.setData(accountListMap);
        }else {
            r.setCode(500);
            r.setMessage("查询失败");
        }
        return r;
    }

    //冻结账户
    @CacheEvict(value = "accountList",allEntries = true)
    @Override
    public R updateAccountFrozen(AccountList accountList) {
        accountList.setStatus((byte)0);
        int result = accountListMapper.updateById(accountList);
        R r = new R();
        if (result > 0) {
            r.setCode(200);
            r.setMessage("修改成功");
        }else {
            r.setCode(500);
            r.setMessage("修改失败");
        }
        return r;
    }

    //解冻账户
    @CacheEvict(value = "accountList",allEntries = true)
    @Override
    public R updateAccountThaw(AccountList accountList) {
        accountList.setStatus((byte)1);
        int result = accountListMapper.updateById(accountList);
        R r = new R();
        if (result > 0) {
            r.setCode(200);
            r.setMessage("修改成功");
        }else {
            r.setCode(500);
            r.setMessage("修改失败");
        }
        return r;
    }

    //根据id查询账户
    @Override
    public R selectAccountById(String id) {

        return null;
    }

    //添加账户
    @CacheEvict(value = "accountList",allEntries = true)
    @Override
    public R addAccount(AccountList accountList) {
        accountList.setCreateTime(new Date());
        int insert = accountListMapper.insert(accountList);
        R r = new R();
        if (insert > 0) {
            r.setCode(200);
            r.setMessage("添加成功");
        }else {
            r.setCode(500);
            r.setMessage("添加失败");
        }
        return r;
    }

//    @Cacheable(value = "accountList",key = "'selectAccountList'")
    @Override
    public R selectAccount(Integer currentPage, Integer pageSize, AccountList accountList) {
        Page page = new Page(currentPage,pageSize);
        QueryWrapper qw = new QueryWrapper();
        qw.eq(accountList.getId() != null,"id",accountList.getId())
                .eq(accountList.getAccountType() != null,"account_type",accountList.getAccountType())
                .eq(accountList.getNickname() != null,"nickname",accountList.getNickname())
                .eq(accountList.getPhone() != null,"phone",accountList.getPhone())
                .eq(accountList.getStatus() != null,"status",accountList.getStatus())
                .between(accountList.getStartTime() != null&&accountList.getEndTime() != null,"create_time",accountList.getStartTime(),accountList.getEndTime())
                .lt(accountList.getStartTime() == null && accountList.getEndTime() != null,"create_time",accountList.getEndTime());
        Page selectPage = accountListMapper.selectPage(page, qw);
        R r = new R();
        if (selectPage.getRecords() != null) {
            Map accountListMap = new HashMap();
            accountListMap.put("totalSize",selectPage.getTotal());
            accountListMap.put("totalPage",selectPage.getPages());
            accountListMap.put("totalData",selectPage.getRecords());
            r.setCode(200);
            r.setMessage("查询成功");
            r.setData(accountListMap);
        }else {
            r.setCode(500);
            r.setMessage("查询失败");
        }
        return r;
    }


    //根据手机查询账户
    @Override
    public AccountList selectAccountByPhone(String phone){
        QueryWrapper qw = new QueryWrapper();
        qw.eq("phone",phone);
        AccountList accountList = accountListMapper.selectOne(qw);
        return accountList;
    }

    //扣除余额
    public int updateAccountBalance(String phone, BigDecimal withdrawalAmount){
        AccountList accountList = selectAccountByPhone(phone);
        AccountList accountList1 = new AccountList();
        accountList1.setId(accountList.getId());
        accountList1.setAccountBalance(withdrawalAmount);
        int i = accountListMapper.updateById(accountList1);
        return i;
    }


}
